<?php
/*=============================================================================
|| ##################################################################
||	phpFoX Konsort
|| ##################################################################
||
||	Copyright		: (C) 2005-2007 PHPFOX Limited a Reality Fox Creation
||	Contact			: info@phpfox.com
||
||	- phpFoX and all of its source code and files are protected by Copyright Laws.
||
||	- The license for phpFoX permits you to install this software on a single domain (.com, .org, .net, etc.).
||
||	- You may also not remove this copyright screen which shows the copyright information and credits for phpFoX (RealityFOX Creations).
||
||	- phpFoX Konsort is NOT a FREE software - http://www.phpfox.com/license/
||
|| ##################################################################
=============================================================================*/

/* $File: ./include/modules/Menu/classes/PhpFox_ComponentMenu.class.php, $Author:  $ */



/** Component to create simple menu
 *
 * Parameters:
 *  'aMenuItems' - menu items array ('pagename'=>'title', or 'pagename' => array(...) )
 *  'sTemplate' - template name ('Menu', 'TabbedMenu')
 *  'aParams'   - additional params to create url for menu items
 *
 *
 */
class PhpFox_Mod_Menu_ComponentMenu extends BaseComponent
{
    function process()
    {
    	$sTemplate = $this->getParam('sTemplate', 'Menu');

        $sPageName = App::getPageName();
        $aParams = $this->getParam('aParams', array());
        foreach($this->_aParams['aMenuItems'] as $k=>$v)
        {
            if (!is_array($v))
            {
                $this->_aParams['aMenuItems'][$k] = array
                (
                    'url'   => App::makeUrl($k, $aParams),
                    'title' => $v,
                );
            }
            else
            {
                if (!isset($v['url']))
                {
                    $this->_aParams['aMenuItems'][$k] = array
                    (
                        'url' => App::makeUrl($k, isset($v['params']) ? $v['params'] : array()),
                    );
                    unset($v['url']);
                }
                $this->_aParams['aMenuItems'][$k] = array_merge($this->_aParams['aMenuItems'][$k], $v);
            }
        }

        //find highlighted menu item by part of page name
        $sTruncPage = $sPageName;

		// Routine to over-ride the highlight setting of a page
		foreach(App::getParam('Mod_Menu.switch_heighlight') as $sCachedKey => $sCachedValue)
		{
			if ( $sTruncPage == $sCachedKey )
			{
				$sTruncPage = $sCachedValue;
				break;
			}
		}

		(($sHook = App::getHook('MenuComponentMenu')) ? eval($sHook) : false);
		
		while($sTruncPage)
        {
            if ($sTruncPage == 'public.gallery.top')
            {
        		$this->_aParams['aMenuItems']['public.gallery.rate']['is_active'] = true;
        		break;
            }
        	if (isset($this->_aParams['aMenuItems'][$sTruncPage]))
            {
                if ($sTruncPage == 'public.videos')
                {        
            		$oReq =& App::getRequest();
            		if ($oReq->get('reported') == 'all')
            		{
                		$this->_aParams['aMenuItems']['public.videos.reported_all']['is_active'] = true;
                		break;
            		}
                }                
            	$this->_aParams['aMenuItems'][$sTruncPage]['is_active'] = true;
                break;
            }
            $sTruncPage = $this->_truncPage($sTruncPage);
            if ($sTruncPage === true)
            {
                $bSetActive = false;
                if ($sCompareParam = $this->getParam('compare_param'))
                {
                	if ( $sPageName == 'public.browse' )
        			{
                		$oReq = &App::getRequest();
                		$sMode = $oReq->get('mode');
					}
                    $oUrl = &App::getUrl();
                    $aPageParams = $oUrl->getParams();
                    foreach ($this->_aParams['aMenuItems'] as $sParam => $aValue)
                    {
                        $sItemUrl = $aValue['url'];
						if ( $sPageName == 'public.browse' )
        				{
                        	if ( !empty($sMode) && eregi($sMode, $aValue['url']) )
                			{
                				$this->_aParams['aMenuItems'][$sParam]['is_active'] = true;
                				$bSetActive = true;
                				break;
	                		}
                        }
                        if (isset($aValue['params']))
                        {
                            $aItemUrlParams = $aValue['params'];
                        }
                        else
                        {
                            $oUrl->_parseUrl($sItemUrl);
                            $aItemUrlParams = $oUrl->getParams();
                        }
                        if ($this->_paramEqual($sCompareParam, $aItemUrlParams, $aPageParams))
                        {
                            $this->_aParams['aMenuItems'][$sParam]['is_active'] = true;
                            $bSetActive = true;
                            break;
                        }
                    }
                }
                if ($bSetActive)
                {
                    break;
                }
                else
                {
                    reset($this->_aParams['aMenuItems']);
                    $sKey = key($this->_aParams['aMenuItems']);
                    $this->_aParams['aMenuItems'][$sKey]['is_active'] = true;
                    break;
                }
            }
        }
        $oReq = &App::getRequest();
        $oTpl = $this->_oModule->getTemplate();
        $sTitle = $this->getParam('sTitle', '');

        $oTpl->assign(array(
            'aMenuItems' => $this->getParam('aMenuItems'),
        	'sTitle'     => ($sTitle ? str_replace(array(" ", "-"), array("&nbsp;", "&nbsp;"), $sTitle) : ''),
        	'sSearchUrl' => $this->getParam('sSearchUrl', ''),
        	'sSearchResults' => $oReq->get('search'),
        ));

        $srcHtml = $oTpl->fetch($sTemplate.'.html');
        return $srcHtml;
    }

    function _truncPage($sPage)
    {
        $iPos = strrpos($sPage, '.');
        if ($iPos!==false)
        {
            return substr($sPage, 0, $iPos);
        }
        else
        {
            return true;
        }
    }

    function _paramEqual($sParamName, $aParams1, $aParams2)
    {
        if (!isset($aParams1[$sParamName]) || !isset($aParams2[$sParamName]))
        {
            return false;
        }
        if ($aParams1[$sParamName] == $aParams2[$sParamName])
        {
            return true;
        }
        return false;
    }
}
?>